package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 组合总和
 * @author qulq
 * @date 2024-07-07 14:10:28
 */
public class T39_CombinationSum{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T39_CombinationSum().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {

	List<List<Integer>> ans = new ArrayList<>();
	List<Integer> path = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
		Arrays.sort(candidates);
		backtracking(candidates, target, 0, 0);
		return ans;
    }

	public void backtracking(int[] candidates, int target, int start, int sum) {
		if (sum == target) {
			ans.add(new ArrayList<>(path));
			return;
		}
		for (int i = start; i < candidates.length && sum + candidates[i] <= target; i++) {
			path.add(candidates[i]);
			backtracking(candidates, target, i, sum + candidates[i]);
			path.remove(path.size() - 1);
		}
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}